題意為給整數 numRows,回傳一個包含前 numRows 層的 Pascal Triangle 的 List。
每一層的數字是由上一層相鄰兩數相加而來。
class Solution {
fun generate(numRows: Int): List<List<Int>> {
val triangle = mutableListOf<List<Int>>()
for (i in 0 until numRows) {
var row = MutableList(i+1) {1}
for (j in 1 until i) {
row[j] = triangle[i-1][j-1]+triangle[i-1][j]
}
triangle.add(row)
}
return triangle
}
}
class Solution {
fun getRow(rowIndex: Int): List<Int> {
val row = MutableList(rowIndex+1) {0}
row[0] = 1
for (i in 1..rowIndex) {
for (j in i downTo 1) {
row[j] = row[j] + row[j-1]
}
}
return row
}
}
// 120 triangle minimum path sum
// 62 Unique Paths
// 1641 Count Sorted Vowel Strings
// 931 Minimum Falling Path Sum
// 1269 Number of Ways to Stay in the Same Place After Some Steps
// 3463 Check If Digits Are Equal in String After Operations II (hard)
// 2221 Find Triangular Sum of an Array